package cn.yeyiu.sexyiu.activity;

import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.ImageView;
import android.widget.SearchView;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import cn.yeyiu.sexyiu.R;
import cn.yeyiu.sexyiu.adapter.VideoListAdapter;
import cn.yeyiu.sexyiu.listener.OnItemClickListener;
import cn.yeyiu.util.MediaUtil;
import cn.yeyiu.util.VideoUtil;
import cn.yeyiu.util.bean.VideoInfo;
import cn.yeyiu.videoplayer.VideoPlayerActivity;
import okhttp3.HttpUrl;

public class SearchActivity extends AppCompatActivity {
    private VideoListAdapter adapter;
    private ImageView backIV;
    private ImageView btnNext;
    private ImageView btnPre;
    private View itemPage;
    private Spinner optionSpin;
    private RecyclerView recyclerView;
    private SearchView searchView;
    private TextView tvCur;
    private TextView tvMax;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_search);
        dataInit();
        viewInit();
        adapterInit();
        eventInit();
        this.itemPage.setVisibility(View.GONE);
    }

    private void dataInit() {
        this.adapter = new VideoListAdapter(this);
    }

    private void viewInit() {
        this.optionSpin = (Spinner) findViewById(R.id.option_spin);
        this.searchView = (SearchView) findViewById(R.id.search_view);
        this.backIV = (ImageView) findViewById(R.id.back_btn);
        this.recyclerView = (RecyclerView) findViewById(R.id.video_content);
        this.btnPre = (ImageView) findViewById(R.id.pre_page_btn);
        this.btnNext = (ImageView) findViewById(R.id.next_page_btn);
        this.tvCur = (TextView) findViewById(R.id.cur_page_tv);
        this.tvMax = (TextView) findViewById(R.id.sum_page_tv);
        this.itemPage = findViewById(R.id.page_ll);
    }

    private void adapterInit() {
        Display defaultDisplay = getWindowManager().getDefaultDisplay();
        if (defaultDisplay.getWidth() > defaultDisplay.getHeight()) {
            this.recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, 1));
        } else {
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
            this.recyclerView.setLayoutManager(linearLayoutManager);
        }
        this.recyclerView.setAdapter(this.adapter);
    }

    private void eventInit() {
        this.adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemLongClick(View view) {
            }

            @Override
            public void onItemClick(View view) {
                int childAdapterPosition = recyclerView.getChildAdapterPosition(view);
                VideoInfo videoInfo = adapter.videoInfoList.getValue().get(
                        adapter.cur_page * adapter.page_size + childAdapterPosition
                );

                MediaUtil.playVideo(SearchActivity.this, VideoPlayerActivity.class, videoInfo);
            }
        });
        this.adapter.videoInfoList.observe(this, videoInfos -> {
            this.adapter.update();
            String maxPage = this.adapter.getMaxPage();
            if (!maxPage.equals("0")) {
                this.tvMax.setText(maxPage);
                this.itemPage.setVisibility(View.VISIBLE);
                this.searchView.setQuery(HttpUrl.FRAGMENT_ENCODE_SET, false);
                this.searchView.setIconified(true);
            } else {
                this.itemPage.setVisibility(View.GONE);
            }
            synchronized (this) {
                notify();
            }
        });
        this.searchView.setIconified(false);
        this.searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            public boolean onQueryTextChange(String str) {
                return false;
            }

            public boolean onQueryTextSubmit(String str) {
                SearchActivity.this.recyclerView.requestFocus();
                String obj = SearchActivity.this.optionSpin.getSelectedItem().toString();
                if (HttpUrl.FRAGMENT_ENCODE_SET.equals(str)) {
                    return true;
                }
                VideoUtil.searchVideoList(str, obj, SearchActivity.this.adapter.videoInfoList);
                SearchActivity.this.showSearchingView();
                return true;
            }
        });
        this.searchView.setOnSearchClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                SearchActivity.this.itemPage.setVisibility(View.GONE);
            }
        });
        this.searchView.setOnCloseListener(new SearchView.OnCloseListener() {
            public boolean onClose() {
                if (!SearchActivity.this.adapter.getMaxPage().equals("0")) {
                    SearchActivity.this.itemPage.setVisibility(View.VISIBLE);
                }
                return false;
            }
        });
        this.backIV.setOnClickListener(v -> {
            finish();
        });
        this.btnPre.setOnClickListener(v -> {
            this.adapter.prePage();
            this.tvCur.setText(this.adapter.getPage());
        });
        this.btnNext.setOnClickListener(v -> {
            this.adapter.nextPage();
            this.tvCur.setText(this.adapter.getPage());
        });
    }

    private void showSearchingView() {
        AlertDialog create = new AlertDialog.Builder(this).setView(R.layout.dialog_searching_videos).setCancelable(false).create();
        create.show();
        new Thread(() -> {
            try {
                synchronized (this) {
                    wait();
                    create.dismiss();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
